package com.ymatou.iapi.livebuyer.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.livebuyer.parameter.*;
import com.ymatou.iapi.livebuyer.service.*;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.sqlwapper.LiveRoomWapper;
import com.ymttest.database.sqlwapper.YmtLoggingWapper;
/**
*退出直播间
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QuitGroup{
	private static QuitGroupBean quitgroupBean;
	private static QuitGroupCall quitgroupCall;
	private static LiveRoomWapper liveRoomWapper;
	private static Integer liveid;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("退出直播间");
	}

	@Before
	public void caseUp() {
		quitgroupBean=new QuitGroupBean();
		quitgroupCall=new QuitGroupCall();
		liveRoomWapper=new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(quitgroupCall.getOpurl());
		liveRoomWapper=null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(QuitGroupBean quitgroupBean,boolean isonline,boolean isview) {
		int liveid=quitgroupBean.getLiveId();
		int uid=quitgroupBean.getUserId();
		Logger.comment("验证Livemember");
		List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
		Logger.verifyEquals(1, templist.size(),"验证有记录");
		if(templist.size()>0){
			Map alivemember = liveRoomWapper.selectLivemember(liveid, uid).get(0);
			Logger.verifyEquals(0, alivemember.get("Status"),"验证状态");
		}
		
		
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuitGroup_001() {
		Logger.start(true,"退出直播间成功");
		try {
			if (liveid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveid = livedto.getId();
			}
			Integer uid = 1;
			LiveRoomCallService.enterGroup(liveid, uid);
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
			
			//是否在线
			boolean isonline=false;
			//是否观看
			boolean isview=false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			 Map blivemember=null;
			 if(templist.size()>0){
				 blivemember=liveRoomWapper.selectLivemember(liveid, uid).get(0);
				 Logger.debug(blivemember);
				 isview=true;
				 if(blivemember.get("Status").equals(1)){
					 isonline=true;
				 }
			 }
			 
			quitgroupBean.setLiveId(liveid);
			quitgroupBean.setUserId(uid);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("200", quitgroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(quitgroupBean,isonline,isview);
			Logger.comment("验证日志数据");
			Map m=new YmtLoggingWapper().selectLiveBehaviorById(liveid, uid);
			Logger.debug(m);
			Logger.verifyEquals(2, m.get("BehaviorType"), "验证BehaviorType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QuitGroup_002() {
		Logger.start(true,"不输入liveid失败");
		try {
//			quitgroupBean.setLiveId(liveid);
			quitgroupBean.setUserId(1);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("100", quitgroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的直播编号", quitgroupCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QuitGroup_003() {
		Logger.start(true,"不输入userid失败");
		try {
			quitgroupBean.setLiveId(1);
//			quitgroupBean.setUserId(1);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("100", quitgroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的用户编号", quitgroupCall.getString("message"), "验证message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_QuitGroup_004() {
		Logger.start(true,"输入不存在liveid失败");
		try {
			quitgroupBean.setLiveId(-1);
			quitgroupBean.setUserId(1);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("100", quitgroupCall.getString("code"), "验证code");
			Logger.verifyIsContains("无效的直播编号", quitgroupCall.getString("message"), "验证message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuitGroup_005() {
		Logger.start(true,"未进入退出直播间");
		try {
			if (liveid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveid = livedto.getId();
			}
			Integer uid = 1;
			//删除记录
			liveRoomWapper.deleteLivemember(liveid, uid);
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
			
			//是否在线
			boolean isonline=false;
			//是否观看
			boolean isview=false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			 Map blivemember=null;
			 if(templist.size()>0){
				 blivemember=liveRoomWapper.selectLivemember(liveid, uid).get(0);
				 Logger.debug(blivemember);
				 isview=true;
				 if(blivemember.get("Status").equals(1)){
					 isonline=true;
				 }
			 }
			 
			quitgroupBean.setLiveId(liveid);
			quitgroupBean.setUserId(uid);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("200", quitgroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			templist = liveRoomWapper.selectLivemember(liveid, uid);
			Logger.verifyEquals(0, templist.size(),"验证无记录");
			Logger.comment("验证liveroom");
			liveroom = liveRoomWapper.selectLiveRoomById(liveid);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuitGroup_006() {
		Logger.start(true,"重复退出直播间");
		try {
			if (liveid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveid = livedto.getId();
			}
			Integer uid = 1;
			//调用一次退出保证未进入
			LiveRoomCallService.enterGroup(liveid, uid);
			LiveRoomCallService.quitGroup(liveid, uid);
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
			
			//是否在线
			boolean isonline=false;
			//是否观看
			boolean isview=false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			 Map blivemember=null;
			 if(templist.size()>0){
				 blivemember=liveRoomWapper.selectLivemember(liveid, uid).get(0);
				 Logger.debug(blivemember);
				 isview=true;
				 if(blivemember.get("Status").equals(1)){
					 isonline=true;
				 }
			 }
			 
			quitgroupBean.setLiveId(liveid);
			quitgroupBean.setUserId(uid);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("200", quitgroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(quitgroupBean,isonline,isview);		
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuitGroup_007() {
		Logger.start(true,"游客退出直播间成功");
		try {
			if (liveid == null) {
				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
				liveid = livedto.getId();
			}
			Integer uid = -1;
			LiveRoomCallService.enterGroup(liveid, uid);
			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
			
			//是否在线
			boolean isonline=false;
			//是否观看
			boolean isview=false;
			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
			 Map blivemember=null;
			 if(templist.size()>0){
				 blivemember=liveRoomWapper.selectLivemember(liveid, uid).get(0);
				 Logger.debug(blivemember);
				 isview=true;
				 if(blivemember.get("Status").equals(1)){
					 isonline=true;
				 }
			 }
			 
			quitgroupBean.setLiveId(liveid);
			quitgroupBean.setUserId(uid);
			quitgroupCall.setData(quitgroupBean);
			quitgroupCall.callService();			
			Logger.verifyEquals("200", quitgroupCall.getString("code"), "验证code");
			LiveRoomCallService.PublishLiveStatCall(liveid);
			verifyDB(quitgroupBean,isonline,isview);
			Logger.comment("验证日志数据");
			Map m=new YmtLoggingWapper().selectLiveBehaviorById(liveid, uid);
			Logger.debug(m);
			Logger.verifyEquals(2, m.get("BehaviorType"), "验证BehaviorType");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//冻结围观人数不是在线人数
//	@Test
//	@Category({P1.class})
//	@TestCase
//	public void Tc_QuitGroup_007() {
//		Logger.start(true,"直播结束冻结人数");
//		try {
//			if (liveid == null) {
//				CreateSellerLiveBean initcreatebean = LiveRoomCallService.initCreatesellerliveBean(null);
//				LiveRoom livedto = LiveRoomCallService.CreateSellerLive(initcreatebean);
//				liveid = livedto.getId();
//			}
//			Integer uid = 1;
//			LiveRoomCallService.enterGroup(liveid, uid);
//			LiveRoom liveroom = liveRoomWapper.selectLiveRoomById(liveid);
//			
//			//是否在线
//			boolean isonline=false;
//			//是否观看
//			boolean isview=false;
//			List<Map> templist = liveRoomWapper.selectLivemember(liveid, uid);
//			 Map blivemember=null;
//			 if(templist.size()>0){
//				 blivemember=liveRoomWapper.selectLivemember(liveid, uid).get(0);
//				 Logger.debug(blivemember);
//				 isview=true;
//				 if(blivemember.get("Status").equals(1)){
//					 isonline=true;
//				 }
//			 }
//			 LiveRoomCallService.PublishLiveStatCall(null);
//			 Integer oonline=(Integer)liveRoomWapper.selectLiveRoomStatById(liveid).get("OnLineNum");
//			 
//			 LiveRoomCallService.AutoCloseLiveRoomByUserId(uid);
//			 
//			quitgroupBean.setLiveId(liveid);
//			quitgroupBean.setUserId(uid);
//			quitgroupCall.setData(quitgroupBean);
//			quitgroupCall.callService();			
//			Logger.verifyEquals("200", quitgroupCall.getString("code"), "验证code");
//			LiveRoomCallService.PublishLiveStatCall(null);
//			 Integer nonline=(Integer)liveRoomWapper.selectLiveRoomStatById(liveid).get("OnLineNum");
//			 Logger.verifyEquals(nonline, oonline, "验证在线人数冻结");
//		} catch (Exception e) {
//			Logger.fail(e);
//		}
//	}
}